package com.zixue.heima.helloNetty.EventLoopGroopDemo;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.Scanner;

/**
 * @ClassName : HelloClient
 * @Description : TODO
 * @Author : chunxiong
 * @Date : 2025/5/2 00:25
 * @Version 1.0
 */
@Slf4j
public class HelloClientFutureChannel2 {
    public static void main(String[] args) throws InterruptedException {
/*
        ChannelFuture channelFuture = new Bootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //调用日志级别handler
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                        ch.pipeline().addLast(new StringEncoder());
                    }
                })
                .connect(new InetSocketAddress("localhost", 8080));
        channelFuture.sync();
        Channel channel = channelFuture.channel();
        new Thread(()->{
            Scanner scanner = new Scanner(System.in);
            while (true){
                String next = scanner.next();
                if ("q".equals(next)){
                    channel.close(); //close异步
                    log.debug("调用close");
                    break;
                }else {
                    channel.writeAndFlush(next);
                }

            }
        }).start();// 线程忘记start 启动没效果
        //方法1
   */
/*     log.debug("处理关闭之后的请求");
        ChannelFuture closeFutre = channel.closeFuture();
        log.info("让住线程阻塞");
        closeFutre.sync(); //同步阻塞 close之后
        log.info("方式1阻塞结束");*//*


        //方法2
        ChannelFuture closeFutre = channel.closeFuture();
        closeFutre.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                log.info("方式2异步结束");
            }
        });
*/

        //3 单这样关闭 没有把线程停掉
        NioEventLoopGroup group = new NioEventLoopGroup();
        ChannelFuture channelFuture = new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //调用日志级别handler
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                        ch.pipeline().addLast(new StringEncoder());
                    }
                })
                .connect(new InetSocketAddress("localhost", 8080));
        channelFuture.sync();
        Channel channel = channelFuture.channel();
        new Thread(()->{
            Scanner scanner = new Scanner(System.in);
            while (true){
                String next = scanner.next();
                if ("q".equals(next)){
                    channel.close(); //close异步
                    log.debug("调用close");
                    break;
                }else {
                    channel.writeAndFlush(next);
                }

            }
        }).start();// 线程忘记start 启动没效果
        //方法1
   /*     log.debug("处理关闭之后的请求");
        ChannelFuture closeFutre = channel.closeFuture();
        log.info("让住线程阻塞");
        closeFutre.sync(); //同步阻塞 close之后
        log.info("方式1阻塞结束");*/

        //方法2
        ChannelFuture closeFutre = channel.closeFuture();
        closeFutre.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture channelFuture) throws Exception {
                log.info("方式2异步结束");
                group.shutdownGracefully(); //优雅关闭
            }
        });


    }
}
